
#ifndef PAINTHANDLER_H
#define PAINTHANDLER_H

#include <BSet>
#include <BCircle>
#include <BIcon>
#include <BImage>
#include <BPicker>
#include <BHandler>
#include <BSurface>
#include <BTerrain>

using namespace BWE;

class PaintHandler : public BHandler
{
public:
	PaintHandler();
	~PaintHandler();

	void setBrushColor(const BColor& color);
	const BColor& brushColor() const;

	void setBrushImage(BImage* brushImage);
	BImage* brushImage() const;

	void setBrushSize(int size);
	int brushSize() const;

	void setPointVisible(bool pointVisible);
	bool pointVisible() const;

protected:
	bool intersectTest(const BQuad& quad, const BTriangle& trig);
	void catchPixels(const BQuad& quad);
	void paintPixels(const BQuad& quad);

protected:
	void slotAssetChanged(BObject* object, const BValue& value);
	
protected:
	virtual void paint(BPainter& painter);
	virtual void render(BRender& render);

	virtual void updateEvent(const BEvent& event);

	virtual void keyPressEvent(const BKeyEvent& keyEvent);
	virtual void keyReleaseEvent(const BKeyEvent& keyEvent);

	virtual void mousePressEvent(const BMouseEvent& mouseEvent);
	virtual void mouseReleaseEvent(const BMouseEvent& mouseEvent);
	virtual void mouseMoveEvent(const BMouseEvent& mouseEvent);

	virtual void mouseClickEvent(const BMouseEvent& mouseEvent);

private:
	BImageHolder		_brushImage;
	int					_brushSize;
	bool				_pointVisible;

	BPickHitHolder		_pickHit;
	BCircle				_brushCircle;
	BQuad				_brushQuad;
	BColor				_brushColor;

	bool				_pressed;
	bool				_shiftDown;
	bool				_ctrlDown;
	bool				_altDown;

	struct PointInfo
	{
		char			state;
		BIntSet		links;
		BIntSet			faces;
	};
	BHolder<BTerrain>	_terrain;
	BArray<PointInfo>	_pointInfos;
	BArray<BIntArray>	_facePixels;
	BArray<BVector>		_pixelPoints;
	BRealArray			_pixelRadius;
	BIntSet				_facesCatched;
};

#endif